'''
功能层：封装业务流程；
'''

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import  sys

sys.path.append(r'..')
from web_base.base_page import BasePage

class DiscuzPage(BasePage):
    url = r'http://192.168.89.128/upload'
    #   登录
    name_loc = (By.ID, 'ls_username')
    pwd_loc = (By.ID, 'ls_password')
    cilck_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em')
    # 注册
    register_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[4]/a')
    username_loc = (By.ID, 'b9xPJS')
    password_loc = (By.ID, 'bL99hQ')
    password1_loc = (By.ID, 'EMzQRH')
    email_loc = (By.ID, 'yPs4kr')
    submit_loc = (By.ID, 'registerformsubmit')

    # # 回复帖子
    # default_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    # post_loc = (By.ID, 'newspecial')
    # title_loc = (By.XPATH, '//*[@id="subject"]')
    # self.driver.switch_to.frame('e_iframe')
    # content_loc = (By.XPATH, '/html/body')
    # self.driver.switch_to.default_content()
    # post1_loc = (By.ID, 'postsubmit')
    #
    #看帖
    default_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    post_loc = (By.XPATH, '//*[@id="normalthread_70"]/tr/th/a[2]')
    # 回帖
    default2_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    post2_loc = (By.XPATH, '//*[@id="normalthread_70"]/tr/th/a[2]')
    content1_loc = (By.ID, 'fastpostmessage')
    submit1_loc = (By.ID, 'fastpostsubmit')

    def __init__(self,driver):
        super().__init__(driver)
        self.get_url(self.url)

    def login(self,username, password):
        self.input_ele(self.name_loc, username)
        self.input_ele(self.pwd_loc, password)
        self.ele_click(self.cilck_loc)
        sleep(3)
    def register(self,username,password,pwd,email):
        self.ele_click(self.register_loc)
        self.input_ele(self.username_loc, username)
        self.input_ele(self.password_loc, password)
        self.input_ele(self.password1_loc, pwd)
        self.input_ele(self.email_loc, email)
        self.ele_click(self.submit_loc)
        sleep(3)


    def read(self,username='admin', password='123456'):
        self.login(username, password)
        self.ele_click(self.default_loc)
        self.ele_click(self.post_loc)
        sleep(3)

    def post(self,title,content,username='admin', password='123456'):

        self.login(username,password)
        self.ele.click(self.default_loc)
        self.ele.click(self.post_loc)
        self.input_ele(self.title_loc, title)
        self.input_ele(self.content_loc, content)
        self.ele.click(self.post1_loc)
        sleep(3)

    def reply(self,content1,username='admin', password='123456'):
        self.login(username, password)
        self.ele_click(self.default2_loc)
        self.ele_click(self.post2_loc)
        self.input_ele(self.content1_loc, content1)
        self.ele_click(self.submit1_loc)
        sleep(3)


if __name__ == '__main__':
    # username = 'admin'
    # password  = ' 123456'

    username = 'asd'
    password = ' 123456'
    pwd = ' 123456'
    email = '123432@163.com'

    content1 = '啦啦啦啦啦'

    chrome_location = r'D:\RJCS\谷歌浏览器内网通紫域和驱动安装\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)
    discuz_page = DiscuzPage(driver)
    # 登录
    #discuz_page.assert(username, password)
    # 注册
    #discuz_page.register(username,password,pwd,email)
    # 看帖
    #discuz_page.read()
    # 回复帖子
    discuz_page.reply(content1)